package de.amaly.wosdemdes.ui;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import de.amaly.wosdemdes.R;
import de.amaly.wosdemdes.activity.CreateEditShowProgress;
import de.amaly.wosdemdes.activity.ShowsListActivity;
import de.amaly.wosdemdes.data.ShowProgressEntry;

/**
 * A custom ArrayAdapter for defining the rows of the show list.
 * 
 * @author Andreas Maly
 * 
 */
public class ShowProgressEntryArrayAdapter extends ArrayAdapter<ShowProgressEntry> {

    /** The values that represent the data of this adapter. */
    private final List<ShowProgressEntry> values;


    /**
     * Constructs a new {@link ShowProgressEntryArrayAdapter}
     * 
     * @param context
     * @param values
     */
    public ShowProgressEntryArrayAdapter(Context context, List<ShowProgressEntry> values) {
        super(context, R.layout.show_progress_entry_row_layout, values);
        this.values = values;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.show_progress_entry_row_layout, parent, false);

        final ShowProgressEntry currentRowEntry = values.get(position);

        TextView textView = (TextView) rowView.findViewById(R.id.rowTextShowName);
        textView.setText(convertTextForDisplay(currentRowEntry.getShowName(), false));

        textView = (TextView) rowView.findViewById(R.id.rowTextSeasonNumber);
        textView.setText(convertTextForDisplay(currentRowEntry.getSeasonNumber(), false));

        textView = (TextView) rowView.findViewById(R.id.rowTextEpisodeNumber);
        textView.setText(convertTextForDisplay(currentRowEntry.getEpisodeNumber(), true));

        textView = (TextView) rowView.findViewById(R.id.rowTextDiscNumber);
        textView.setText(convertTextForDisplay(currentRowEntry.getDiscNumber(), true));

        textView = (TextView) rowView.findViewById(R.id.rowTextProgressComment);
        textView.setText(convertTextForDisplay(currentRowEntry.getProgressComment(), false));

        // Hide the ProgressComment TextView if no progress comment is defined
        if (StringUtils.isBlank(textView.getText())) {
            rowView.findViewById(R.id.layout_to_hide).setVisibility(View.GONE);
        }

        ImageButton button = (ImageButton) rowView.findViewById(R.id.showRowButton);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), CreateEditShowProgress.class);
                intent.putExtra(ShowsListActivity.EXTRA_SHOW_PROGRESS_ENTRY, currentRowEntry);
                ((Activity) getContext()).startActivityForResult(intent, ShowsListActivity.EDIT_SHOW_PROGRESS_REQUEST);
            }
        });

        return rowView;
    }


    private String convertTextForDisplay(Object input, boolean emptyReplacement) {

        String inputString = input == null ? null : input.toString();

        if (StringUtils.isBlank(inputString)) {
            return emptyReplacement ? "-" : null;
        } else {
            return inputString.trim();
        }
    }
}